package com.zlp.spring.tx.service.v1.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;

@Component
public class TransactionalUtils {


    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    
    /** 
     * 开启事务
     * @date: 2022/2/24 13:23
     * @return: org.springframework.transaction.TransactionStatus 
     */
    public TransactionStatus beginTransaction(){
       return dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
    }


    /**
     * 提交事务
     * @param transactionStatus
     * @date: 2022/2/24 13:24
     * @return: void
     */
    public void commit(TransactionStatus transactionStatus){
        dataSourceTransactionManager.commit(transactionStatus);
    }

    /**
     * 回滚事务
     * @param transactionStatus
     * @date: 2022/2/24 13:24
     * @return: void
     */
    public void rollback(TransactionStatus transactionStatus){
        dataSourceTransactionManager.rollback(transactionStatus);
    }


}
